package com.fancon.book.soixich;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FanconBookActivity extends Activity {
	/** Called when the activity is first created. */
	static final String[] CHAPTERS = new String[] { "Chương I    : Mở đầu",
													"Chương II   : Cuối tuần anh đưa tôi...",
													"Chương III  : Chiều nay ngồi buồn tôi nhớ đến...",
													"Chương IV   : Tôi tò mò mở cuốn băng ra xem...",
													"Chương V    : Vào đêm đó, vẫn như thường lệ...",
													"Chương VI   : Có tiếng kêu nho nhỏ...",
													"Chương VII  : Sau ngày mê hồn ấy, tôi như...",
													"Chương VIII : Kết thúc"
													 };
	
	
	// Menu option
		private int gotoBookmarkMenu = Menu.FIRST;  
	    private int aboutMenu = Menu.FIRST + 1;  
	    private int groupId = 1; 
	    @Override  
	    public boolean onCreateOptionsMenu(Menu menu) {  
	      menu.add(groupId, gotoBookmarkMenu , gotoBookmarkMenu, getResources().getString(R.string.menu_gotoBookmark)).setIcon(R.drawable.gobookmark);  
	      menu.add(groupId, aboutMenu , aboutMenu, getResources().getString(R.string.menu_about)).setIcon(R.drawable.info);  
	      return true; 
	    }  

		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			
			switch (item.getItemId()) {
			case Menu.FIRST:
				
				//Open bookmarked chapter
				Intent intent = new Intent(getApplicationContext(), ChapterDetail.class);
				String chapter = PreferenceUtil.getChapter(getApplicationContext(), PreferenceUtil.PREFS_KEY, "-1");
				if (!chapter.equals("-1")) {
					intent.putExtra("chapter", Integer.parseInt(chapter));
					intent.putExtra("title", CHAPTERS[Integer.parseInt(chapter)]);
					Log.d("chapter", Integer.parseInt(chapter) + "");
					startActivity(intent);
				} else {
					showDialog(getString(R.string.cannot_go_bookmark));
				}
				break;
			case Menu.FIRST + 1:
				// About author
				doAbout();
				break;
			

			}
			return true;
		}
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chapters);
		setTitle(R.string.chapter_title);
		ListView lv = (ListView) findViewById(R.id.chapter_lst);
		lv.setCacheColorHint(0);
		lv.setAdapter(new ArrayAdapter<String>(this, R.layout.chapter_item,
				CHAPTERS));
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getApplicationContext(), ChapterDetail.class);
				intent.putExtra("chapter", arg2);
				intent.putExtra("title", CHAPTERS[arg2]);
				Log.d("chapter", arg2 + "");
				startActivity(intent);
			}
		});

	}
	private void doAbout() {
		try {
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setData(Uri.parse("market://search?q=pub:Fancon+Software"));
			startActivity(intent);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	/*
	 * show dialog when have a error
	 */
	public void showDialog(String msg) {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg).setCancelable(true)
				.setNegativeButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}
}